﻿/*
 *GreyBox was written by Strategic Data Systems and licenced under the open source MS-PL. 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Diagnostics;

namespace StrategicDataSystems.Cloud.Monitoring.UI.Services
{

    public class RefreshTimerService : ITimerService
    {
        private DispatcherTimer _refreshTimer;

        /// <summary>
        /// Initializes a new instance of the RefreshTimerService class.
        /// </summary>
        public RefreshTimerService()
        {
            _refreshTimer = new DispatcherTimer();
        }

        public void Start(int intervalInMinutes, EventHandler timerEvent)
        {
            if (_refreshTimer.IsEnabled)
            {
                _refreshTimer.Stop();
            }

            _refreshTimer.Tick += timerEvent;
            _refreshTimer.Interval = new TimeSpan(0, intervalInMinutes, 0);
            _refreshTimer.Start();
            Trace.WriteLine(string.Format("Refresh Timer initialized with a {0} minute interval.", intervalInMinutes.ToString()), "System");
        }

        public void Stop()
        {
            Trace.WriteLine("Refresh Timer Stop called", "System");
            if (_refreshTimer != null)
            {
                _refreshTimer.Stop();
            }
        }

        public void Pause()
        {
            Trace.WriteLine("Refresh Timer Pause called", "System");
            if (_refreshTimer != null)
            {
                _refreshTimer.IsEnabled = false;
            }
        }

        public void Resume()
        {
            Trace.WriteLine("Refresh Timer Resume called.", "System");
            if (_refreshTimer != null)
            {
                _refreshTimer.IsEnabled = true;
            }
        }


        private void cleanupTimer()
        {
            Stop();
        }
    }

}
